﻿namespace suit_test
{
    using System;
    using System.IO;
    using System.Collections.Generic;
    public class KTest : suit_base.SuiteBase
    {
        Dictionary<string, List<string>> m_fileCollect =
            new Dictionary<string, List<string>>();
        CExpFile m_expFile;
        public override string GetCommand()
        {
            return ET_Improve.Global.CMD_TEST;
        }
 
        public override void Call()
        {
            string sSamplePath = "F:\\表格样张";
            Console.WriteLine("Todo Test with:" + sSamplePath);
            Console.WriteLine("Press any key..");
            Console.ReadKey();
            Dictionary<string, List<tool.CAddInfoHlp.CFileInfo>> fileDic =
                tool.CAddInfoHlp.GetFolderFiles(sSamplePath, ET_Improve.Global.CMD_TEST, ".xls|.xlsx|.et");
            foreach (string sDir in fileDic.Keys)
            {
                foreach (tool.CAddInfoHlp.CFileInfo fi in fileDic[sDir])
                {
                    AddFileMD5Info(fi.FileName, GetFileMD5(fi.FileName));
                }
            }

            m_expFile.Begin("F:\\suit_test.txt");
            DoRedundancy();
            m_expFile.End();
        }

        protected string GetFileMD5(string sFileName)
        {
            System.IO.FileStream fs = new System.IO.FileStream(sFileName,
                System.IO.FileMode.Open);
            System.Security.Cryptography.MD5 md5 = 
                System.Security.Cryptography.MD5.Create();
            byte[] bs = md5.ComputeHash(fs);
            fs.Close();
            return System.BitConverter.ToString(bs).Replace("-", "");
        }

        protected void AddFileMD5Info(string sFileName, string sMD5)
        {
            if(m_fileCollect.ContainsKey(sMD5))
            {
                m_fileCollect[sMD5].Add(sFileName);
            }
            else
            {
                List<string> lst = new List<string>();
                lst.Add(sFileName);
                m_fileCollect.Add(sMD5, lst);
            }
        }

        protected void DoRedundancy()
        {
            try
            {
                foreach (string md5 in m_fileCollect.Keys)
                {
                    List<string> lst = m_fileCollect[md5];
                    System.Diagnostics.Debug.Assert(lst.Count >= 1);
                    if (lst.Count == 1)
                    {
                        continue;
                    }

                    ChooseReserveFile(lst);
                    foreach (string s in lst)
                    {
                        m_expFile.AddRecord(s);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        // 选择要保留的项目，并删除其他相同的文件，并做日志记录
        // lst中删除待保留的项目
        protected void ChooseReserveFile(List<string> lst)
        {
            Console.WriteLine();
            Console.WriteLine("Choose the file sequence Number will be reserved?");
            for (int i = 0; i <= lst.Count; ++i)
            {
                Console.WriteLine(string.Format("{0}. {1}", i, lst[i]));
            }
            int nSelSeq;
            do
            {
                nSelSeq = -1;
                Console.Write("Please Choose:");
                string sInput = Console.ReadLine();
                if (IsNumber(sInput))
                {
                    nSelSeq = Int32.Parse(sInput);
                }
            } while (nSelSeq > 0 && nSelSeq <= lst.Count);
            lst.RemoveAt(nSelSeq);
            // 真正的删除了
            foreach (string s in lst)
            {
                try
                {
                    Console.WriteLine("Delte: " + s);
                    File.Delete(s);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("Failed of Delete! " + ex.ToString());
                    Console.WriteLine("Press Any key...");
                    Console.ReadKey();
                }
            }
        }

        protected bool IsNumber(string s)
        {
            foreach (char ch in s)
            {
                if (!Char.IsNumber(ch))
                {
                    return false;
                }
            }
            return true;
        }
    }

    internal class CExpFile
    {
        StreamWriter m_sw;
        public void Begin(string sOutput)
        {
            m_sw = new StreamWriter(sOutput);
        }

        public void AddRecord(string sFileName)
        {
            m_sw.WriteLine(sFileName);
        }

        public void End()
        {
            if (m_sw != null)
            {
                m_sw.Close();
                m_sw = null;
            }
        }
    }
}